#include "dpLibrary\dpWindows.h"
#include "winlib.h"

void RegisterWindowsClass(WNDPROC proc)
{
  //Register the window class for the game.
  WNDCLASSEX wc = {sizeof(WNDCLASSEX),  //The size of this structure (passing the size allows Microsoft to update their interfaces and maintain backward compatibility)
    CS_CLASSDC,  //The style of the window class--this is the base type (one device context for all windows in the process)
    proc,  //The name of the message handling function
    0L, 0L,  //The amount of extra memory to allocate for this class and window
    GetModuleHandle(NULL),  //Handle to the instance that has the windows procedure--NULL means use this file.
    NULL,  //Add an Icon as a resource and add them here
    LoadCursor(NULL, IDC_ARROW),  //Use the default arrow cursor
    NULL, NULL,  //The background brush and menu--these can be NULL
    WindowsClassName, NULL};  //The class name and the small icon (NULL just uses the default)

  RegisterClassEx(&wc);
}

HWND CreateInstance(HINSTANCE instance, UINT width, UINT height)
{
  //Create the game's window
  return  CreateWindow(WindowsClassName, //The class name
    WindowTitle, //The name for the title bar
    WS_OVERLAPPEDWINDOW, //The style of the window (WS_BORDER, WS_MINIMIZEBOX, WS_MAXIMIZE, etc.)
    CW_USEDEFAULT, CW_USEDEFAULT, //The x and y position of the window (screen coords for base windows, relative coords for child windows)
    width, //Width of the window, including borders
    height, //Height of the window, including borders and caption
    GetDesktopWindow(), //The parent window
    NULL, //The menu for the window
    instance, //The handle to the instance of the window (ignored in NT/2K/XP)
    NULL); //The lParam for the WM_CREATE message of this window
}


//Process any windows messages and run the game until we get a quit message
//While we don't use the window handle, in other cases we might want to only process messages for this window
void WindowsSystemUpdate()
{
  MSG msg;
  //Look for any pending windows messages, remove them, then handle them
  //The second parameter is the window handle--NULL just means get any message from the current thread
  //The third and forth parameters are the start and end message types to process
  //The last parameter determines whether or not the message is removed
  while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) //It is important to get all windows messages available not just one
  {
    TranslateMessage(&msg); //Makes sure WM_CHAR and similar messages are generated
    DispatchMessage(&msg); //Calls the message procedure (see below) with this message
  }
}


HWND InitializeWindows(HINSTANCE instance, WNDPROC proc, int clientWidth, int clientHeight)
{
  HWND hWnd = 0;

  //The size passed to CreateWindow is the full size including the windows 
  //border and caption AdjustWindowRect will adjust the provided rect so that 
  //the client size of the window is the desired size
  RECT fullWinRect = {0, 0, clientWidth, clientHeight};
  AdjustWindowRect(&fullWinRect, //The rectangle for the full size of the window
    WS_OVERLAPPEDWINDOW, //The style of the window
    FALSE); //Does this window have a menu.

  RegisterWindowsClass(proc);

  hWnd = CreateInstance(instance, fullWinRect.right-fullWinRect.left, fullWinRect.bottom-fullWinRect.top);
  ShowWindow(hWnd, SW_SHOWDEFAULT);

  return hWnd;
}